﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IndoorInformation
{
    public partial class frmDanhSachPhongBan : Form
    {
        public frmDanhSachPhongBan()
        {
            InitializeComponent();
        }

        protected DataTable _GridDataSource
        {
            get
            {
                if (gridControl1.DataSource is DataTable)
                    return (gridControl1.DataSource as DataTable);
                else if (gridControl1.DataSource is DataView)
                    return (gridControl1.DataSource as DataView).ToTable();
                else return null;
            }
        }

        private void frmDanhSachThongTinSuKien_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = DanhMucRoom.Instance.DataTable;

            foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView1.Columns)
            {
                if (column.FieldName == "ID") column.Visible = false;
            }
        }

        private void Edit()
        {
            try
            {
                int[] tmp = gridView1.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    object row = gridView1.GetRow(selectedRowIndex);
                    if (row != null && row is DataRowView)
                    {
                        int id = (int)((DataRowView)row)[0];
                        frmEditPhongBan frm = new frmEditPhongBan(DanhMucRoom.Instance.GetByID(id));
                        frm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmEditPhongBan frm = new frmEditPhongBan(null);
            frm.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Edit();
        }
        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn có chắc muốn xóa bản ghi này", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.No) return;

                int[] tmp = gridView1.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    object row = gridView1.GetRow(selectedRowIndex);
                    if (row != null && row is DataRowView)
                    {
                        int id = (int)((DataRowView)row)[0];
                        DanhMucRoom.Instance.GetByID(id).DeleteFromDB();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
